package com.laowang.project.system.controller;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

import com.laowang.common.utils.DateUtil;
import com.laowang.common.utils.DateUtils;
import com.laowang.project.system.domain.vo.UserDetailVo;
import com.laowang.project.system.domain.vo.ZcUserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.laowang.framework.aspectj.lang.annotation.Log;
import com.laowang.framework.aspectj.lang.enums.BusinessType;

import org.springframework.web.bind.annotation.*;
import com.laowang.project.system.domain.ZcActivity;
import com.laowang.project.system.service.ZcActivityService;
import com.laowang.framework.web.controller.BaseController;
import com.laowang.framework.web.domain.AjaxResult;
import com.laowang.common.utils.poi.ExcelUtil;
import com.laowang.framework.web.page.TableDataInfo;
import oshi.jna.platform.mac.SystemB;

/**
 * 众筹活动Controller
 * 
 * @author laowang
 * @date 2025-09-09
 */
@Api(tags = "众筹活动接口")
@RestController
@RequestMapping("/zcActivity")
public class ZcActivityController extends BaseController {
    @Autowired
    private ZcActivityService zcActivityService;


    @ApiOperation(value = "H5端 获取众筹活动详细信息",notes = "")
    @GetMapping(value = "/web/{id}")
    public AjaxResult webId(@PathVariable("id") Integer id) {

        ZcActivity zcActivity = zcActivityService.selectZcActivityDetailById(id);
        if (zcActivity == null) {
            return  AjaxResult.success("活动不存在");
        }
        if (zcActivity.getStatus().equals(0)) {
            return  AjaxResult.success("活动已下线");
        }
        //人数 平均  剩余天数
        List<ZcUserVo> users = zcActivity.getUsers();
        users.forEach(user -> {
            String s = DateUtils.getDateList(user.getPayTime(), new Date());
            user.setDesc(s);

        });
        Map<Integer, List<ZcUserVo>> map = new HashMap<>();
        for (ZcUserVo user : users) {
            if (map.isEmpty()){
                List<ZcUserVo> list = new ArrayList<>();
                list.add(user);
                map.put(user.getUserId(),list);
            }else {
                if (map.containsKey(user.getUserId())) {
                    map.get(user.getUserId()).add(user);
                }else  {
                    List<ZcUserVo> list = new ArrayList<>();
                    list.add(user);
                    map.put(user.getUserId(),list);
                }
            }
        }
        List<UserDetailVo> userDetailList = new ArrayList<>();
        for (Integer i : map.keySet()) {
            UserDetailVo userDetailVo = new UserDetailVo();
            userDetailVo.setUserId(i);
            List<ZcUserVo> zcUserVos = map.get(i);
            userDetailVo.setZcUserVos(zcUserVos);
            BigDecimal gongchou = BigDecimal.ZERO;
            for (ZcUserVo zcUserVo : zcUserVos) {
                gongchou = gongchou.add(zcUserVo.getTotalPrice());
            }
            userDetailVo.setGongchou(gongchou);

            List<ZcUserVo> supportList = zcUserVos.stream().filter(zcUserVo -> zcUserVo.getSupportId() != null).collect(Collectors.toList());
            userDetailVo.setSupportList(supportList);
            userDetailList.add(userDetailVo);
        }
        zcActivity.setUserDetailList(userDetailList);
        Integer renshu = users.size();
        BigDecimal pingjun = BigDecimal.ZERO;
        if (renshu > 0) {
            pingjun = zcActivity.getHaveAmount().divide(new BigDecimal(renshu), 2, BigDecimal.ROUND_HALF_UP);

        }
        zcActivity.setPingjun(pingjun);
        zcActivity.setRenshu(renshu);
        long l = DateUtil.dayDiff(new Date(), zcActivity.getEndTime());
        zcActivity.setRaminDay(l+1L);
        Integer count =  zcActivityService.selectCountOrder(id);
        zcActivity.setCanyucishu(count);


        return AjaxResult.success(zcActivity);
    }


    /**
     * 查询众筹活动列表
     */
    @ApiOperation(value = "查询众筹活动列表",notes = "")
    @PreAuthorize("@ss.hasPermi('zc:zcActivity:list')")
    @GetMapping("/list")
    public TableDataInfo list(ZcActivity zcActivity) {
        startPage();
        List<ZcActivity> list = zcActivityService.selectZcActivityList(zcActivity);
        return getDataTable(list);
    }
    
    /**
     * 导出众筹活动列表
     */
    @ApiOperation(value = "导出众筹活动列表",notes = "")
    @PreAuthorize("@ss.hasPermi('zc:zcActivity:export')")
    @Log(title = "众筹活动", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ZcActivity zcActivity) {
        List<ZcActivity> list = zcActivityService.selectZcActivityList(zcActivity);
        ExcelUtil<ZcActivity> util = new ExcelUtil<ZcActivity>(ZcActivity.class);
        return util.exportExcel(list, "zcActivity");
    }

    /**
     * 获取众筹活动详细信息
     */
    @ApiOperation(value = "获取众筹活动详细信息",notes = "")
    @PreAuthorize("@ss.hasPermi('zc:zcActivity:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(zcActivityService.selectZcActivityById(id));
    }

    /**
     * 新增众筹活动
     */
    @ApiOperation(value = "新增众筹活动",notes = "")
    @PreAuthorize("@ss.hasPermi('zc:zcActivity:add')")
    @Log(title = "众筹活动", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZcActivity zcActivity) {
        return toAjax(zcActivityService.insertZcActivity(zcActivity));
    }

    /**
     * 修改众筹活动
     */
    @ApiOperation(value = "修改众筹活动",notes = "")
    @PreAuthorize("@ss.hasPermi('zc:zcActivity:edit')")
    @Log(title = "众筹活动", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZcActivity zcActivity) {
        return toAjax(zcActivityService.updateZcActivity(zcActivity));
    }

    /**
     * 删除众筹活动
     */
    @ApiOperation(value = "删除众筹活动",notes = "")
    @PreAuthorize("@ss.hasPermi('zc:zcActivity:remove')")
    @Log(title = "众筹活动", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(zcActivityService.deleteZcActivityByIds(ids));
    }
}
